package by.grsu.transport.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import by.grsu.transport.form.RegistrationForm;
import by.grsu.transport.model.User;
import by.grsu.transport.service.ServiceManager;

public class RegistrationAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RegistrationForm registrationForm = (RegistrationForm) form;

		String targer = null;

		try {
			if (registrationForm != null) {
				ServiceManager serviceManager = ServiceManager.getDefault();

				User user = new User();
				user.setName(registrationForm.getFirstName() + " "
						+ registrationForm.getLastName());
				user.setLogin(registrationForm.getLogin());
				user.setPassword(registrationForm.getPassword());
				user.setIsLocked(false);
				user.setRole(User.Role.PASSENGER);

				serviceManager.getRegistrationService().registerUser(user);
				targer = ForwardActionTo.SUCCESS;
			} else {
				targer = ForwardActionTo.FAIL;
			}
		} catch (Exception e) {
			targer = ForwardActionTo.FAIL;
		}
		return mapping.findForward(targer);
	}

}
